using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Collections;

namespace QLThuHocPhi
{
    public partial class ListMonHoc : Form
    {
       // DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();
        QLThuHocPhi.Database.DBTool dbt = new QLThuHocPhi.Database.DBTool();
        DataSet ds = new DataSet();
        //int hocphi = 0;
        public ListMonHoc()
        {
            InitializeComponent();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            dbt.loadform(QLThuHocPhi.Database.DBTool.username);
            string tenvn = QLThuHocPhi.Database.DBTool.quyen;
            if (!tenvn.Equals("Admin"))
            {
                MessageBox.Show("Bạn không có quyền Admin");
                return;
            }
            try
            {
                if (MessageBox.Show("Bạn có chắc muốn xóa môn " + txtTenMon.Text + " không?", "thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    int mamon = int.Parse(txtMaMon.Text);
                    dbt.deleteMonhoc(mamon);
                    LoadData(0);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SystemForm sf = new SystemForm();
            sf.Show();
            this.SetVisibleCore(false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dbt.loadform(QLThuHocPhi.Database.DBTool.username);
            string tenvn = QLThuHocPhi.Database.DBTool.quyen;
            if (!tenvn.Equals("Admin"))
            {
                MessageBox.Show("Bạn không có quyền Admin");
                return;
            }
            try{
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    string a = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    if (a.Equals(txtTenMon.Text))
                    {
                        MessageBox.Show("Môn học đã tồn tại");
                        return;
                    }
                }

            }
            catch (Exception) { }
                try
                {
                    string a = txtTenMon.Text;
                    int k = int.Parse(txtSotinchi.Text);
                    if (MessageBox.Show("Bạn có chắc chắn muốn thêm môn học này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        dbt.insertmonhoc(a, k);
                        for (int i = 0; i < listBox2.Items.Count; i++)
                        {

                            dbt.inserkhoamon1(a, listBox2.Items[i].ToString());
                        }
                        LoadData(0);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        
           
        }
        private void LoadMonHoc() { 
       
        }
       
         
        private void ListMonHoc_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'qLHocPhiDataSet.Khoa' table. You can move, or remove it, as needed.
           // this.khoaTableAdapter.Fill(this.qLHocPhiDataSet.Khoa);
            LoadData(0);
            LoadCombo();
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i=listBox2.Items.Count-1;i>=0;i--) {
                listBox2.Items.RemoveAt(i);
            }
            try
            {
                int mamon = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                ds = dbt.Khoa_monhoc(mamon);
               // listBox2.DataSource = ds.Tables["Khoa_monhoc"];
                //listBox2.DisplayMember = "Tenkhoa";
                for (int i = 0; i < ds.Tables["Khoa_monhoc"].Rows.Count; i++) {
                    listBox2.Items.Add(ds.Tables["Khoa_monhoc"].Rows[i].ItemArray[0].ToString());
                }
            }
            catch (Exception )
            {

            }
            txtMaMon.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            txtTenMon.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            txtSotinchi.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            txtMahocphi.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
        }
        private void LoadData(int makhoa) {
            ds = dbt.GetListMonHoc(makhoa);
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "MonHoc";
            dataGridView1.Columns[0].HeaderText = "Mã môn";
            dataGridView1.Columns[1].HeaderText = "Tên môn học";
            dataGridView1.Columns[2].HeaderText = "Số tín chỉ";
            dataGridView1.Columns[3].HeaderText = "Mã học phí";
            dataGridView1.Columns[4].HeaderText = "Số tiền";
            //dataGridView1.Columns[5].HeaderText = "Học phí";
            LoadCombo();
        }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
           // int i = int.Parse(cbtimkiemkhoa.SelectedValue.ToString());
            //LoadData(i);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            
            
           int i = int.Parse(cbtimkiemkhoa.SelectedValue.ToString());
            LoadData(i);
            cbtimkiemkhoa.SelectedValue = i;
            for (int k = listBox2.Items.Count - 1; k >= 0; k--)
            {
               listBox2.Items.RemoveAt(k);
            }
        }
        private void ClearListbox2() {
            try
            {
                for (int i = listBox2.Items.Count - 1; i >= 0; i--)
                {
                    listBox2.Items.RemoveAt(i);
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
        private void LoadCombo()
        {
            DataSet ds1 = new DataSet();
            ds1 = dbt.LoadTenKhoa();
            DataSet ds2 = new DataSet();
            ds2 = dbt.LoadTenkhoa1();
            this.listBox1.DataSource = ds2.Tables["tenk1"];
            this.listBox1.DisplayMember = "Tenkhoa";
            this.listBox1.ValueMember = "MaKhoa";
            this.cbtimkiemkhoa.DataSource = ds1.Tables["tenk"];
            this.cbtimkiemkhoa.DisplayMember = "Tenkhoa";
            this.cbtimkiemkhoa.ValueMember = "MaKhoa";
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

           
        }

        private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string b = listBox1.Text;
          for (int i = 0; i < listBox2.Items.Count; i++)
                {
                    if (b.Equals(listBox2.Items[i].ToString()))
                    {
                      
                        return;
                    }
                }
          
           // listBox2.DataSource = null;
            listBox2.Items.Add(b);
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string b = listBox2.Text;
            
            listBox2.Items.Remove(b);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            dbt.loadform(QLThuHocPhi.Database.DBTool.username);
            string tenvn = QLThuHocPhi.Database.DBTool.quyen;
            if (!tenvn.Equals("Admin"))
            {
                MessageBox.Show("Bạn không có quyền Admin");
                return;
            }
            try
            {
                int a = int.Parse(txtMaMon.Text);
                int k = int.Parse(txtSotinchi.Text);
                if (MessageBox.Show("Bạn có chắc chắn muốn sửa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    dbt.EditMonhoc(a, txtTenMon.Text, k);
                    dbt.DeleteKhoamon(a);
                    for (int i = 0; i < listBox2.Items.Count; i++)
                    {
                        // MessageBox.Show(listBox2.Items[i].ToString());
                        dbt.inserkhoamon(txtTenMon.Text, listBox2.Items[i].ToString());
                    }
                    LoadData(0);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void txtTenMon_Click(object sender, EventArgs e)
        {
            txtTenMon.SelectAll();
        }
    }
}